﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace yyycommon
{
    public class NetUtil
    {
        private static bool isNetEnable;
        public static bool IsNetEnable() {
            if (isNetEnable)
            {
                return true;
            }
            try
            {
                WebClient client = new WebClient();
                client.DownloadString("https://www.baidu.com/favicon.ico");
                isNetEnable = true;
            }
            catch (Exception)
            {
                isNetEnable = false;
            }
            return isNetEnable;
        }

        #region 发送post请求
        class JsonResp {
            public object data = null;
            public int code = 0;
            public string msg ="";
            public string time ="";
        }
        public static string Post(string url,string postData,string contentType)
        {
            string result = "";

            if (!IsNetEnable())
            {
                return result;
            }
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                if (!string.IsNullOrEmpty(postData))
                {
                    req.ContentType = contentType;
                    byte[] data = Encoding.UTF8.GetBytes(postData);//把字符串转换为字节
                    req.ContentLength = data.Length; //请求长度
                    using (Stream reqStream = req.GetRequestStream()) //获取
                    {
                        reqStream.Write(data, 0, data.Length);//向当前流中写入字节
                        reqStream.Close(); //关闭当前流
                    }
                }

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //响应结果
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
            }
            catch (Exception e) {
                Console.WriteLine("network exception {0}" , e.Message);
            }
            return result;
        }

        public static string PostStr(string url, string postData) {
            return Post(url, postData, "application/x-www-form-urlencoded");
        }
        public static object PostJson(string url,object data) {
            string resp = null;
            if (data != null)
            {
                string postData = JsonConvert.SerializeObject(data);
                resp = Post(url, postData, "application/json;charset=UTF-8");
            }
            else {
                resp = Post(url, "", "");
            }
            if (string.IsNullOrEmpty(resp)) {
                return null;
            }
            JsonResp respObj = JsonConvert.DeserializeObject<JsonResp>(resp);
            if (respObj != null) {
                if (respObj.code != 0) {
                    Console.WriteLine("响应异常:{0},msg = {1}",respObj.code,respObj.msg);
                }
                return respObj.data;
            }
            return null;
        }
        #endregion

    }
}
